<?php

$addr = address();
$rel_addr = relative_address();

// OB_START
ob_start();

/*
**  S I T E   V A R I A B L E S
*/
$SITE_NAME = 'Non-Profit Exchange';

/*
**   S E S S I O N S
*/
include $rel_addr.'/sessions/login_session.php';
include $rel_addr.'/sessions/profile_session.php';
include $rel_addr.'/sessions/search_session.php';

/*
** I N C L U D E   F I L E S
*/
	// Database Connection
	include $rel_addr.'/lib/mysql_connect.php';
	
	// Header File
	include $rel_addr.'/lib/header.php';
	
	// Scripts File
	include $rel_addr.'/lib/scripts.php';
	
	// Navigation File
	include $rel_addr.'/lib/navigation.php';
	
	// Footer File
	include $rel_addr.'/lib/footer.php';

/*
**   M Y S Q L 
*/
	function db_connect ($db_url, $db, $user, $pw){
		if($dbc = @mysql_connect($db_url,$user,$pw)){
			if (!@mysql_select_db ($db)){
				return die('<p>Could not select the database because: <B>' . mysql_error() . '');
			}else{
				return $dbc;
			}
		}else{
				return die('<P>Could not connect to the MYSQL because: <B>' . mysql_error() . '');
		}
	}

/*
**   P H P
*/
	function address (){
		return 'http://'.$_SERVER['SERVER_NAME'].'/npexchange';
	}
	function relative_address(){
		return '/home/rafael/npexchange';
	}

	function is_ie (){
		//Not Internet Explorer
		if(stristr($_SERVER['HTTP_USER_AGENT'], 'MSIE') === FALSE) {
			return false;
		//Is Internet Explorer
		}else{
			return true;
		}
	}
	
	function make_unique_id($string){
		$unique_id = strlen($string);
		$y = 3;
		$string = ' '.$string;
		$size = strlen($string);
		
		for($x = 1; $x < $size; $x++){
			if($x % 2 == 0){
				$y+= (ord($string[$x])*$x);
			}else{
				$y+= (int)(ord($string[$x]) / $x) + (int)(ord($string[$x]) / 2);
			}
			$unique_id += $x*ord($string[$x]) + (10*$x) + $y;
		}
		return $unique_id;
	}
	function member_type($type){
		switch ($type){
			
			case 'IND':
				return 'Individual';
				break;
			case 'NPO':
				return 'Non-Profit Organization';
				break;
			case 'BUS':
				return 'Business';
				break;
		}
	}

	//Retrieves variable page from URL
	function this_content (){
		return $_REQUEST['page'];
	}
	
	//Retrieves page's name i.e. index.php
	function this_page(){
	
		// Returns /page.php
	
		/*
		** $_SERVER['PHP_SELF'] needs to be cleaned up if its in a folder
		** it'll return /folder/file.php if file.php is in the folder called folder
		** use this, if the root of your website is in a sub-folder
		*/
		
		$string = $_SERVER['PHP_SELF'];
		// remove the subfolder
		$pos    = strpos($string,'/',1);
		$return = substr($string,$pos);
		
		return $return;
		
		/*
		** If the root of your site is not in a sub-folder
		** use this :
		** return $_SERVER['PHP_SELF'];
		*/
	}
	
	// Goto if object is clicked
	function click_get($var_name, $var){
		return 'OnClick="parent.location=\''.this_page().'?'.$var_name.'='.$var.'\'"';
	}
	
	// Refresh if item is selected
	function click_refresh($page = 'default'){
		if($page == 'default'){
			return 'OnClick="parent.location=\''.this_page().'\'"';
		}else {
			return 'OnClick="parent.location=\''.$page.'\'"';
		}
	}
	
	// Submit form on click
	function click_submit(){
		return 'OnClick="this.form.submit()"';
	}
	
	// checks if a user session exists
	// if not, refreshes to index.php
	function login_check(){
		global $m_user_id;
		if(empty($m_user_id)){
			echo 'You must be logged in to view this page.<br/>Refreshing in 2 seconds.';
			refresh('/index.php','none',2);
			exit;
		}
	}

	// Refresh a page
	function refresh ($page = 'none', $dbpage = 'none',$seconds = 0){
		$address = address();
		switch(TRUE){
			case ($page != 'none' && $dbpage != 'none'):
				return header('refresh: '.$seconds.'; url='.$address.$page.'?page='.$dbpage.'');
				break;
			case ($page != 'none'):
				return header('refresh: '.$seconds.'; url='.$address.$page.'');
				break;
			case($dbpage != 'none'):
				return header('refresh: '.$seconds.'; url='.$address.this_page().'?page='.$dbpage.'');
				break;
			case($page == 'none'):
				return header('refresh: '.$seconds.'; url='.$address.this_page().'');
				break;
		}
	}

	function title ($title){
		if(!empty($title)){
			return '<TITLE>'.$title.' - '.$GLOBALS['SITE_NAME'].'</TITLE>';
		}else{
			return '<TITLE>'.$GLOBALS['SITE_NAME'].'</TITLE>';
		}
	}

	function style ($style){
		$reset 	 = '/style/reset.css';
		$header  = '/style/header.css';
		$nav 	 = '/style/nav.css';
		$content = '/style/content.css';
		$footer  = '/style/footer.css';
		$style 	 = '/style/style.css';

		return '<link type="text/css" rel="stylesheet" href="'.address().$reset.'">
			<link type="text/css" rel="stylesheet" href="'.address().$style.'">
			<link type="text/css" rel="stylesheet" href="'.address().$header.'">
			<link type="text/css" rel="stylesheet" href="'.address().$nav.'">
			<link type="text/css" rel="stylesheet" href="'.address().$content.'">
			<link type="text/css" rel="stylesheet" href="'.address().$footer.'">
		';
	}

	function navigation ($nav){
		if(empty($nav)){
			return '
				
				';
		}else{
			return $nav;
		}
	}
	function headCheck ($header){
		if(empty($header)){
			return ''.$GLOBALS['SITE_NAME'].'';
		}else{
			return $header;
		}
	}
	
/*
**    F A C E B O O K
**      C O N N E C T
*/

	function facebook_api(){
	
		// NPExchange
		if($_SERVER['SERVER_NAME'] == 'npexchange.com'){
			return 'db3645d6ecd0455e4f223507267aad15';
		
		// NPE Local
		}else if($_SERVER['SERVER_NAME'] == 'localhost'){
			return '2b7b1b98114a30de117910bf83d8faf0';
		}
	}
	function facebook_secret(){
		// NPExchange
		if($_SERVER['SERVER_NAME'] == 'npexchange.com'){
			return '978df30c22806fdaf149af0db5621fee';
		}else if($_SERVER['SERVER_NAME'] == 'localhost'){
			return 'c07528e1e28135cb308606a413db7cde';
		}
	}
	function facebook_verify($arr){
		$secret = facebook_secret();
		$api = facebook_api();
		$sig = '';
		ksort($arr);
		foreach ($arr as $key => $val) {
			if (substr($key, 0, 33) == $api.'_') {
				$sig .= substr($key, 33) . '=' . $val;
			}
		}
		$sig .= $secret;
		$verify = md5($sig);
		
		$timestamp = date("m/d/y h:i:s");

		$fp = fopen('verify.txt', 'w');
		fwrite($fp, "API: $api \nSecret:$secret \nGenSig:$verify \nSig: {$arr["$api"]} \n$timestamp \n");
		fclose($fp);
		
		if ($verify == $arr["$api"]){
			return true;
		}else{
			return false;
		}
	}
/*
**    P A G E 
**      C R E A T I O N
*/
	function head ($title, $style, $scripts){
		
		$return ='
		<html xmlns:fb="http://www.facebook.com/2008/fbml">
		<head>

		'.title($title).'

		'.style($style).'
		
		'.$scripts.'
		
		</head>
		';
		
		echo $return;
	}

function body ($header,$personal,$subtitle=' ',$content,$navigation,$side=' '){
		$return .= '
		<body>
			<!-- Facebook Stuff -->
			<script src="http://static.ak.connect.facebook.com/js/api_lib/v0.4/FeatureLoader.js.php" type="text/javascript"></script>
		
			<div id="header">
				<div onclick="goHome()" id="title">'.headCheck($header).'</div>
				<div id="personal">'.$personal.'</div>
			</div>
			
			<div id="nav">'.navigation($navigation).'</div>
	
			<div id="content">
				<div id="side">
				'.$side.'
				</div>
				<h1>'.$subtitle.'</h1>
				'.$content.'
			</div>
			';
		echo $return;
	}

	function foot ($footer = 'All Rights Reserved',$dbc){
		$date = date("m/d/Y");

			$return = '
			<div id="footer">
			'.$footer.'
			</div>
			
			<script type="text/javascript">  FB_RequireFeatures(["XFBML"], function(){ FB.Facebook.init("'.facebook_api().'", "connect/xd_receiver.htm"); }); </script>
			
			</body>
	
		
			</html>
			';
		
		echo $return;
		
		ob_end_flush();
		mysql_close($dbc);
	}
	
?>
